package com.snj.test2510;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PlaceRouteUtils {


    /**
     * 从 PlaceRoute 对象中扣除指定地点及其对应的值
     *
     * @param placeRoute  包含 placeWay、stowage 和 totalLoad 的 PlaceRoute 对象
     * @param removePlace 需要扣除的地点
     * @return 更新后的 PlaceRoute 对象
     */
    public static PlaceRouteVoParam removePlaceAndStowageAndTotalLoad(PlaceRoute placeRoute, String removePlace) {
        if (placeRoute == null || removePlace == null) {
            // 如果输入为空，直接返回原始对象
//            throw new CommonException(Constants.ERROR, "数据异常");
        }

        String placeWay = placeRoute.getPlaceWay();
        String stowage = placeRoute.getStowage();
        double totalLoad = placeRoute.getTotalLoad();

        // 将 placeWay 按 "->" 拆分为数组
        String[] places = placeWay.split("->");

        // 排除首尾的 "河西基地" 和 "仓储缓存点"，得到中间的地点数组
        List<String> middlePlaces = new ArrayList<>();
        for (int i = 1; i < places.length - 1; i++) {
            middlePlaces.add(places[i].trim());
        }

        // 将 stowage 按 "," 拆分为数组
        String[] stowages = stowage.split(",");
        List<String> stowageList = new ArrayList<>(Arrays.asList(stowages));

        // 找到需要删除的地点对应的索引
        int removeIndex = middlePlaces.indexOf(removePlace.trim());

        double removedLoad = 0.0; // 记录扣除的值
        if (removeIndex != -1) {
            // 从 middlePlaces 中删除指定地点
            middlePlaces.remove(removeIndex);

            // 从 stowageList 中删除对应索引的值，并记录扣除的值
            removedLoad = Double.parseDouble(stowageList.get(removeIndex).trim());
            stowageList.remove(removeIndex);
        }

        // 重新拼接 placeWay
        StringBuilder newPlaceWay = new StringBuilder();
        // 添加 "河西基地"
        newPlaceWay.append(places[0].trim());
        for (String place : middlePlaces) {
            newPlaceWay.append("->").append(place);
        }
        // 添加 "仓储缓存点"
        newPlaceWay.append("->").append(places[places.length - 1].trim());

        // 重新拼接 stowage
        String newStowage = String.join(", ", stowageList);

        // 更新 totalLoad
        double newTotalLoad = totalLoad - removedLoad;

        // 更新 PlaceRoute 对象的字段

        PlaceRouteVoParam vo = new PlaceRouteVoParam();

        vo.setPlaceWay(newPlaceWay.toString());
        vo.setStowage(newStowage);
        vo.setTotalLoad(newTotalLoad);
        // 被剔除的重量
        vo.setRemovedLoad(removedLoad);

        return vo;
    }

    public static void main(String[] args) {
        // 示例数据
        PlaceRoute placeRoute = new PlaceRoute(
                "河西基地->南通本部->如东->启东->仓储缓存点",
                "0.1687, 1.4482, 2.9738",
                4.59070
        );
        String removePlace = "如东";

        // 调用方法扣除指定地点及其对应的值
        PlaceRouteVoParam result = removePlaceAndStowageAndTotalLoad(placeRoute, removePlace);

        // 输出结果
        System.out.println("原始 placeWay: " + placeRoute.getPlaceWay());
        System.out.println("原始 stowage: " + placeRoute.getStowage());
        System.out.println("原始 totalLoad: " + placeRoute.getTotalLoad());
        System.out.println("扣除 " + removePlace + " 后的 placeWay: " + result.getPlaceWay());
        System.out.println("扣除 " + removePlace + " 后的 stowage: " + result.getStowage());
        System.out.println("扣除 " + removePlace + " 后的 totalLoad: " + result.getTotalLoad());
    }
}

